#!/usr/bin/env python

#Copyright (C) 2008 Luis Felipe Millani (lf.millani@gmail.com)

#This file is part of MagicLyrics.

#MagicLyrics is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#MagicLyrics is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with MagicLyrics.  If not, see <http://www.gnu.org/licenses/>.
from os import name
class Players(object):pass
if name=='posix':
	# i think using dbus like this will create a memory leak
	import dbus
	bus = dbus.SessionBus()
	obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
	iface = dbus.Interface(obj, 'org.freedesktop.DBus')
	
	def is_player_active(player_info): # if player is not active, must return True
		return player_info in iface.ListNames()
	
	class __banshee(Players):
		def info(self):
			return 'org.bansheeproject.Banshee'
		def active(self,info):
			pass
		def playing(self):
			try:
				currentTrack=self.obj.GetCurrentTrack()
			except:
				self.obj=bus.get_object('org.bansheeproject.Banshee', '/org/bansheeproject/Banshee/PlayerEngine')
				currentTrack=self.obj.GetCurrentTrack()
			finally:
				try:
					song=(unicode(currentTrack['artist']),unicode(currentTrack['name']))
				except KeyError:
					return (u'',u'')
				else:
					return song
	
	class __rhythmbox(Players):
		def info(self):
			return 'org.gnome.Rhythmbox'
		def active(self,info):
			pass
		def playing(self):
			try:
				uri=rbplayer.getPlayingUri()
			except:
				rbshell_obj = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
				rbshell = dbus.Interface(rbshell_obj, 'org.gnome.Rhythmbox.Shell')
				rbplayer = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
				rbplayer = dbus.Interface(rbplayer, 'org.gnome.Rhythmbox.Player')
				uri=rbplayer.getPlayingUri()
			finally:
				song = rbshell.getSongProperties(uri)
				return (unicode(song['artist']), unicode(song['title']))
elif name=='nt':
	import win32gui
	
	def is_player_active(player_info):
		try:
			return win32gui.FindWindow(player_info[0],player_info[1])
		except:
			return False
	
	class __musikcube(Players):
		def info(self):
			return ('musikCubeWindow1.0',None)
		def active(self,info):
			self.handle=info
		def playing(self):
			music=win32gui.GetWindowText(self.handle)
			if music[:1]=='[' and music.find(' - ')>-1 and music.rfind(']')>-1:
				artist=music[music.find(' - ')+3:music.rfind(']')]
				music=music[1:music.find(' - ')]
				return (unicode(artist),unicode(music))
			else:
				return (u'',u'')
elif name=='mac':
	pass # sorry guys

class Player(object):
	def __init__(self):
		self.players=map(lambda x:x(),Players.__subclasses__())
		self.findplayer()
	def findplayer(self):
		for player in self.players:
			info=is_player_active(player.info())
			if info:
				player.active(info)
				self.player=player
				return
		self.player=None
	def playing(self):
		"""
		return currently playing music (artist,music)
		or None if it fails
		"""
		if self.player==None or not is_player_active(self.player.info()):
			self.findplayer()
			if self.player==None or not is_player_active(self.player.info()):
				return None
		return self.player.playing()
